/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import com.saville.debug.*;
import com.saville.msgcomp.*;
import com.saville.mc.*;

public class TestMsgPipe1 extends ActiveMc {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestMsgPipe1();
    }

    TestMsgPipe1() {
        super("TestMsgPipe1");

        Msg msg;
        df = Log.VERBOSE;

        mTmp = new TcpMsgPipe("amc://192.168.0.133", this.mMcId.id, 2);
        msg = getMsg(1, McConst.MC_GUID, McConst.MC_CMD_ECHO);
        msg.arg1 = 1;
        mTmp.queueMsg(msg);

        msg = getMsg(1, McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_REGISTER_DEV);
        DevId devId = new DevId();
        devId.id = -1;
        devId.urn = mName;
        devId.friendlyName = "fn-" + mName;
        msg.obj = devId;
        mTmp.queueMsg(msg);

        msg = getMsg(1, McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_LOOKUP_DEV);
        DevId lookupDevId = new DevId();
        lookupDevId.urn = "TestMsgPipe2";
        msg.obj = lookupDevId;
        mTmp.queueMsg(msg);

    }

    @Override
    public void processMsg(Msg pipeMsg) {
        if (pipeMsg.is(McConst.MCPIPE_GUID, McConst.MCPIPE_CMD_PRODUCER_MSG)) {
            Msg msg = (Msg) pipeMsg.obj;

            if (msg.cmdIsReply(McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_REGISTER_DEV)) {
                Log.d(df, "%s: register dev reply status=%d devId %s", mName, msg.status,
                    (DevId) msg.obj);
            } else if (msg.cmdIsReply(McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_LOOKUP_DEV)) {
                DevId devId = (DevId) msg.obj;
                msg.obj = null;

                if (msg.isStatusOk()) {
                    Log.d(df, "%s: forward msg to the devId we successfully found devId %s", mName,
                        devId);
                    pipeMsg = getMsg(1, McConst.MCMASTER_GUID, McConst.MCMASTER_CMD_FORWARD_MSG);
                    pipeMsg.arg1 = devId.id;
                    msg = getMsg(1, McConst.MC_GUID, McConst.MC_CMD_ECHO);
                    pipeMsg.obj = msg;
                    mTmp.queueMsg(pipeMsg);
                } else {
                    Log.d(df, "%s: lookup dev reply status=%d, failed finding devId %s", mName,
                        msg.status, devId);
                }
            } else {
                Log.d(df, "%s: msg status=%d guid=0x%x cmd=0x%x arg1=0x%x", mName, msg.status,
                    msg.guid, msg.cmd, msg.arg1);
            }
        } else {
            Log.e(df, "%s: received unknown pipeMsg status=%d guid=0x%x cmd=0x%x", mName,
                pipeMsg.status, pipeMsg.guid, pipeMsg.cmd);
            if (pipeMsg.cmdIsRequest()) {
                sendReplyMsg(pipeMsg, McConst.STATUS_UNKNOWN_CMD);
            }
        }
    }

    private TcpMsgPipe mTmp;
}
